/**
 * @Description: 账号、地址等配置信息管理
 * @author Beon
 * @date 2021/1/14
*/
import * as fs from 'fs';
import * as path from 'path';

export interface lzgitJson{
    // jira
    username?: string
    password?: string

    // gitlab
    // Authorization?: string

    // config
    jira?: string
    git?: string

    // runConfig
    branchCheck?: boolean
}
type lzgitJsonArray = string[]

export class LzgitCliFile{

    /**
     * 根据传进数组获取数据
     * @param getObj
     * @returns {Promise<{}>}
     */
    static getFileData(getObj?: lzgitJsonArray): lzgitJson {
        const backObj = {};

        try {
            const json = JSON.parse(
                fs.readFileSync(path.resolve(__dirname + '/lzgit-cli.json'), 'utf8')
            ) as lzgitJson;

            if (!getObj) {return json;}

            getObj.forEach((item) => {
                backObj[item] = json[item];
            });

            return backObj;
        } catch (e) {
            return backObj;
        }
    }

    /**
     * 重写json文件内容
     * @param {lzgitJson} coverObj 重写对象数据
     * @returns {Promise<void>}
     */
    static wirteFile(coverObj: lzgitJson): void {
        let json;
        try {
            json = JSON.parse(
                fs.readFileSync(path.resolve(__dirname + '/lzgit-cli.json'), 'utf8')
            ) as lzgitJson;
        } catch (e) {
            json = {};
        }

        Object.keys(coverObj).forEach((item: string) => {
            json[item] = coverObj[item];
        });

        fs.writeFileSync(path.resolve(__dirname + '/lzgit-cli.json'), JSON.stringify(json));
    }
}
